﻿Imports Cls_Common
Imports VSModel
Imports System.Data
Partial Class FrontEnd_Control_Oto_HomeSalon
    Inherits System.Web.UI.UserControl
    Public strSalonURL As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "abc", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "abc", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Page.RouteData.Values("Id") Is Nothing Then
                BindBasicInfo(Page.RouteData.Values("Id"))
            End If
        End If
    End Sub
    Protected Sub BindBasicInfo(ByVal _id As Integer)
        Using _data As New VSEntities
            Dim p = (From q In _data.Stores Where q.StoreId = _id Select q).FirstOrDefault
            If Not p Is Nothing Then
                hplTitle.Text = p.Title
                Page.Title = p.Title
                Page.MetaDescription = p.Description
                '  ltrDiaChi.Text = p.DiaChi
                ltrDescription.Text = p.Description
                'ltrDienThoai.Text = p.Phone
                'If Not p.PhoneOther Is Nothing Then
                '    ltrDienThoai.Text = p.Phone & " - " & p.PhoneOther
                'End If
                hidStoreId.Value = p.UserId
                strSalonURL = p.QueryString & "-" & p.StoreId.ToString & "/"
                BindToGrid(1)
                Dim k = (From q In _data.StoreImages Where q.StoreId = p.StoreId Select q).Take(9).ToList
                If k.Count > 0 Then
                    rptBigBanner.DataSource = k
                    rptBigBanner.DataBind()

                    With rptImageThumb
                        .DataSource = k
                        .DataBind()
                    End With

                End If
            End If


        End Using
    End Sub
    Protected Sub rptBigBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBigBanner.ItemDataBound
        Dim row As StoreImage = e.Item.DataItem
        Dim HighslideImage1 As Encosia.HighslideImage = e.Item.FindControl("HighslideImage1")
        HighslideImage1.ImageUrl = CookedImageURL("420-300", row.ImageURL)
        HighslideImage1.FullImageURL = CookedImageURL("800-600", row.ImageURL)
        If e.Item.ItemIndex = 0 Then
            HighslideImage1.Attributes.Add("style", "display:inline")
        Else
            HighslideImage1.Attributes.Add("style", "display:none")
        End If

    End Sub


    Protected Sub rptImageThumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImageThumb.ItemDataBound
        Dim row As StoreImage = e.Item.DataItem
        Dim imgThumb As Image = e.Item.FindControl("imgThumb")
        imgThumb.ImageUrl = CookedImageURL("90-60", row.ImageURL)

    End Sub
    
    



    Private Sub BindToGrid(ByVal ipage As Integer)
        Using Data As New VSEntities

            Dim count As Integer

            Dim p As List(Of RaoVat)


            p = (From q In Data.RaoVats Where q.UserId = hidStoreId.Value And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() _
                                            Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 15).Take(15).ToList

            count = (From q In Data.RaoVats Where q.UserId = hidStoreId.Value And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() Select q.RaovatId).Count
            'xem nhieu trong tuan

            '   lblTotal.Text = count
            lblTotal2.Text = count
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 15, 10)

            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim ltrTinhTrangXe As Literal = CType(e.Item.FindControl("ltrTinhTrangXe"), Literal)


        ltrTinhTrangXe.Text = getTinhTrangXe(row.RaovatId)

        hplTitle.Text = row.Title.Split("(")(0)
        hplTitle.NavigateUrl = MainDomain & strSalonURL & row.QueryString & "-" & row.RaovatId.ToString & ".html"

        Dim ltrDescription As Literal = CType(e.Item.FindControl("ltrDescription"), Literal)
        ltrDescription.Text = CropText(row.Description, 50, "...")
        Dim ltrUSD As Literal = CType(e.Item.FindControl("ltrUSD"), Literal)
        ltrUSD.Text = "~ " & CType((row.OriginalPrice * 1000000) / TyGia, Double).ToString("#,##0") & " USD"




        Dim imgProduct As Image = CType(e.Item.FindControl("imgProduct"), Image)

        imgProduct.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        'imgProduct.ImageUrl = row.ImagePreview.ToString


        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        ltrPrice.Text = IIf(row.OriginalPrice = 0, "Call", FormatCurrencyOto(row.OriginalPrice.ToString))

        Dim ltrLienHe As Literal = CType(e.Item.FindControl("ltrLienHe"), Literal)

        Dim ltrTelephone As Literal = CType(e.Item.FindControl("ltrTelephone"), Literal)
        ltrLienHe.Text = row.HoTen & Environment.NewLine & row.DiaChi
        ltrTelephone.Text = row.Phone

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrTelephone.Text = ltrTelephone.Text & " - " & row.PhoneOther
        End If

        Dim ltrMaTin As Literal = CType(e.Item.FindControl("ltrMaTin"), Literal)
        ltrMaTin.Text = "Mã: " & row.Code
        Dim ltrTinhThanh As Literal = CType(e.Item.FindControl("ltrTinhThanh"), Literal)
        If Not IsNothing(row.TinhTP) Then
            ltrTinhThanh.Text = GetTinhThanhPho(row.TinhTP)
        End If



    End Sub
    Private Function getTinhTrangXe(ByVal _RaoVatId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.ThongSoCoBans Where q.RaoVatId = _RaoVatId Select q.TinhTrang, q.NamSanXuat).FirstOrDefault
            If Not p Is Nothing AndAlso p.TinhTrang = 1 Then
                Return " Xe mới " & p.NamSanXuat.ToString
            Else
                Return " Xe cũ " & p.NamSanXuat.ToString
            End If
        End Using

    End Function

    Private Function GetTinhThanhPho(ByVal _TinhThanhId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.DonViHanhChinhs Where q.Id = _TinhThanhId Select q.Title).FirstOrDefault
            If Not p Is Nothing Then
                Return p
            End If

        End Using
        Return ""
    End Function

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub


    
    


End Class
